package com.jiegui.modules.common.camera;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.jiegui.common.core.redis.RedisCache;
import com.jiegui.common.utils.StringUtils;
import com.jiegui.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: wuqicheng
 * @time: 2022/6/9 13:32
 */
@Slf4j
public class CameraUtils {

    public static String getSessionId() {
        try {
            String result = HttpUtil.get("http://api.uddgps.com/UserLogin?username=18986117602&password=123456");
            JSONObject json = JSONObject.parseObject(result);
            if ("ok".equals(json.getString("Result"))) {
                return json.getString("SessionId");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("------------请求接口异常，异常信息为" + e);
            return null;

        }
        return null;
    }

    public static String cacheSessionId(){
        RedisCache redisCache=SpringUtils.getBean(RedisCache.class);
        String sessionId = redisCache.getCacheObject("cameraSessionId");
        if(StringUtils.isEmpty(sessionId)){
            sessionId = getSessionId();
            redisCache.setCacheObject("cameraSessionId",sessionId,9, TimeUnit.MINUTES);
        }
        return sessionId;
    }


    /**
     * @Author wuqicheng
     * @Description   获取流地址
     * @Date 15:04 2022/6/9
     * @param  deviceNo:
     * @return com.alibaba.fastjson.JSONObject
     **/
    public static  JSONObject getPushStreamUrl(String deviceNo){
        String sessionId = cacheSessionId();
        String url="http://api.uddgps.com/GetPushStreamUrl?DeviceNo="+deviceNo+"&SessionId="+sessionId;
        String result = HttpUtil.get(url);
        log.info("获取推流接口返回请求为"+result);
        return JSONObject.parseObject(result);
    }
}
